// SPDX-License-Identifier: MIT
// Copyright 2020-2021 NXP
/**
 * @file custom.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include <stdio.h>
#include "lvgl.h"
#include "custom.h"
#include "ScnsInclude.h"

/*********************
 *      DEFINES
 *********************/

/**********************
 *      TYPEDEFS
 **********************/

/**********************
 *  STATIC PROTOTYPES
 **********************/

/**********************
 *  STATIC VARIABLES
 **********************/

/**
 * Create a demo application
 */

void custom_init(lv_ui*ui)
{
    /* Add your codes here */
}

static ScnsStatus getImage(ScnsImage*image,uint8*type)
{
    *type=0;
#if defined(SCNS_OV7725_ENABLE)&&SCNS_OV7725_ENABLE==1
    if(SCNS_STATUS_OK==scnsOv7725GetImage(SCNS_OV7725_1,image))
    {
        *type=1;
        return SCNS_STATUS_OK;
    }
#endif
#if defined(SCNS_OV2640_ENABLE)&&SCNS_OV2640_ENABLE==1
    if(SCNS_STATUS_OK==scnsOv2640GetImage(SCNS_OV2640_1,image))
    {
        *type=2;
        return SCNS_STATUS_OK;
    }
#endif
#if defined(SCNS_OV5640_ENABLE)&&SCNS_OV5640_ENABLE==1
    if(SCNS_STATUS_OK==scnsOv5640GetImage(SCNS_OV5640_1,image))
    {
        *type=3;
        return SCNS_STATUS_OK;
    }
#endif

    return SCNS_STATUS_FAILED;
}

static void finishImage(ScnsImage*image,uint8 type)
{
#if defined(SCNS_OV7725_ENABLE)&&SCNS_OV7725_ENABLE==1
    if(type==1){scnsOv7725SetFinish(SCNS_OV7725_1,image);}
#endif
#if defined(SCNS_OV2640_ENABLE)&&SCNS_OV2640_ENABLE==1
    if(type==2){scnsOv2640SetFinish(SCNS_OV2640_1,image);}
#endif
#if defined(SCNS_OV5640_ENABLE)&&SCNS_OV5640_ENABLE==1
    if(type==3){scnsOv5640SetFinish(SCNS_OV5640_1,image);}
#endif
}

static uint8 keyStatus[3];

void lvglLedKeyHandler(ScnsKeyMsg msg)
{
    if(msg.key==SCNS_KEY_S17){keyStatus[0]=msg.status!=SCNS_KEY_PRESS_UP;}
    else if(msg.key==SCNS_KEY_S11){keyStatus[1]=msg.status!=SCNS_KEY_PRESS_UP;}
    else if(msg.key==SCNS_KEY_S5){keyStatus[2]=msg.status!=SCNS_KEY_PRESS_UP;}
}

void lvgl_update(void)
{
    if(lv_obj_is_valid(guider_ui.button_key17_led))
    {
        if(keyStatus[0])
        {
            lv_led_off(guider_ui.button_key17_led);
        }
        else
        {
            lv_led_on(guider_ui.button_key17_led);
        }
    }
    if(lv_obj_is_valid(guider_ui.button_key11_led))
    {
        if(keyStatus[1])
        {
            lv_led_off(guider_ui.button_key11_led);
        }
        else
        {
            lv_led_on(guider_ui.button_key11_led);
        }
    }
    if(lv_obj_is_valid(guider_ui.button_key5_led))
    {
        if(keyStatus[2])
        {
            lv_led_off(guider_ui.button_key5_led);
        }
        else
        {
            lv_led_on(guider_ui.button_key5_led);
        }
    }
    if(lv_obj_is_valid(guider_ui.temperature_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.temperature_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.light_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.light_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.beep_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.beep_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.camera_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.camera_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.tample_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.tample_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.led_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.led_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.button_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.button_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.touch_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.touch_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.screen_digital_clock_4)){lv_dclock_set_text_fmt(guider_ui.screen_digital_clock_4,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}
    if(lv_obj_is_valid(guider_ui.motor_digital_clock_3)){lv_dclock_set_text_fmt(guider_ui.motor_digital_clock_3,"%02d:%02d:%02d",__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetHour(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetMinute(RTC)),__LL_RTC_CONVERT_BCD2BIN(LL_RTC_TIME_GetSecond(RTC)));}

    if(lv_obj_is_valid(guider_ui.led_sw_l7))
    {
        if(scnsLedGet(SCNS_LED_D7)==SCNS_LED_SWITCH_ON){lv_obj_add_state(guider_ui.led_sw_l7,LV_STATE_CHECKED);}
        else{lv_obj_clear_state(guider_ui.led_sw_l7,LV_STATE_CHECKED);}
    }
    if(lv_obj_is_valid(guider_ui.led_sw_l8))
    {
        if(scnsLedGet(SCNS_LED_D8)==SCNS_LED_SWITCH_ON){lv_obj_add_state(guider_ui.led_sw_l8,LV_STATE_CHECKED);}
        else{lv_obj_clear_state(guider_ui.led_sw_l8,LV_STATE_CHECKED);}
    }
    if(lv_obj_is_valid(guider_ui.led_sw_l9))
    {
        if(scnsLedGet(SCNS_LED_D9)==SCNS_LED_SWITCH_ON){lv_obj_add_state(guider_ui.led_sw_l9,LV_STATE_CHECKED);}
        else{lv_obj_clear_state(guider_ui.led_sw_l9,LV_STATE_CHECKED);}
    }
    if(lv_obj_is_valid(guider_ui.led_sw_l3))
    {
        if(scnsLedGet(SCNS_LED_D3)==SCNS_LED_SWITCH_ON){lv_obj_add_state(guider_ui.led_sw_l3,LV_STATE_CHECKED);}
        else{lv_obj_clear_state(guider_ui.led_sw_l3,LV_STATE_CHECKED);}
    }
    if(lv_obj_is_valid(guider_ui.led_sw_l4))
    {
        if(scnsLedGet(SCNS_LED_D4)==SCNS_LED_SWITCH_ON){lv_obj_add_state(guider_ui.led_sw_l4,LV_STATE_CHECKED);}
        else{lv_obj_clear_state(guider_ui.led_sw_l4,LV_STATE_CHECKED);}
    }
    if(lv_obj_is_valid(guider_ui.led_sw_l5))
    {
        if(scnsLedGet(SCNS_LED_D5)==SCNS_LED_SWITCH_ON){lv_obj_add_state(guider_ui.led_sw_l5,LV_STATE_CHECKED);}
        else{lv_obj_clear_state(guider_ui.led_sw_l5,LV_STATE_CHECKED);}
    }
    if(lv_obj_is_valid(guider_ui.temperature_temp1))
    {
        char buf[128];
        hxSNPrintf(buf,sizeof(buf),"%.1f",scnsDs18b20Result[SCNS_DS18B20_1].temperature);
        lv_label_set_text(guider_ui.temperature_temp1,buf);
    }
    if(lv_obj_is_valid(guider_ui.temperature_temp2))
    {
        char buf[128];
        hxSNPrintf(buf,sizeof(buf),"%d",scnsDht11Result[SCNS_DHT11_1].temperature);
        lv_label_set_text(guider_ui.temperature_temp2,buf);
    }
    if(lv_obj_is_valid(guider_ui.temperature_hum1))
    {
        char buf[128];
        hxSNPrintf(buf,sizeof(buf),"%d",scnsDht11Result[SCNS_DHT11_1].humidity);
        lv_label_set_text(guider_ui.temperature_hum1,buf);
    }
    if(lv_obj_is_valid(guider_ui.light_label_light_val))
    {
        char buf[128];
        hxSNPrintf(buf,sizeof(buf),"%.2f",scnsBh1750Result[SCNS_BH1750_1].lux);
        lv_label_set_text(guider_ui.light_label_light_val,buf);
    }
    {
        ScnsImage image;
        uint8 type;
        static ScnsImage lastImage;
        static uint8 lastType=0;
        if(SCNS_STATUS_OK==getImage(&image,&type))
        {
            const Point plu=scnsLcdGetPhysicalPlu(1);
            const Point prd=scnsLcdGetPhysicalPrd(1);
            static uint64 lastTime;
            uint64 t=scnsTimerGetUs();
            uint32 fps=scnsTimerGetFps(t-lastTime);
            lastTime=t;
            scnsMenuAddLocalConst(2,fps,,SCNS_TYPE_INT32);
            if(type)
            {
                uint64 diff=0;
                if(lastType)
                {
                    diff=scnsImageMinus(&image,&lastImage);
                    t=scnsTimerGetUs()-t;
                    scnsMenuAddLocalConst(2,diff,,SCNS_TYPE_UINT64);
                    scnsMenuAddLocalConst(2,t,,SCNS_TYPE_UINT64);
                    scnsLedSet(SCNS_LED_D17,diff>=5e6?SCNS_LED_SWITCH_ON:SCNS_LED_SWITCH_OFF);
                }
                if(lv_obj_is_valid(guider_ui.camera_label_error))
                {
                    char buf[128];
                    hxSNPrintf(buf,sizeof(buf),"%llu",diff);
                    scnsLcdDisplayImage(SCNS_LCD_LAYER_AUTO,(Point){(plu.x+prd.x)/2-image.width/2,(plu.y+prd.y)/2-image.height},&image,0,0,0);
                    lv_label_set_text(guider_ui.camera_label_error,buf);
                }
            }
            if(lastType)
            {
                finishImage(&lastImage,type);
            }
            lastImage=image;
            lastType=type;
        }
    }
}
